1543C - Need for Pink Slips - CodeForces Solution


bitmasks brute force dfs and similar implementation math probabilities *1900

Please click on ads to support us..

C++ Code:

// Arnav

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <climits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_set>
#include <unordered_map>

using namespace std;

#define int long long
#define pb push_back
#define ff first
#define ss second
#define endl "\n"


unsigned long long power(unsigned long long x, int y, int p)
{
    unsigned long long res = 1; // Initialize result

    x = x % p; // Update x if it is more than or
    // equal to p

    while (y > 0) {

        // If y is odd, multiply x with result
        if (y & 1)
            res = (res * x) % p;

        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
unsigned long long modInverse(unsigned long long n, int p)
{
    return power(n, p - 2, p);
}
unsigned long long mul(unsigned long long x,
                    unsigned long long y, int p)
{
    return x * 1ull * y % p;
}
unsigned long long divide(unsigned long long x,
                        unsigned long long y, int p)
{
    return mul(x, modInverse(y, p), p);
}
unsigned long long nCrModPFermat(unsigned long long n,
                                int r, int p)
{
    // If n<r, then nCr should return 0
    if (n < r)
        return 0;
    // Base case
    if (r == 0)
        return 1;
    // if n-r is less calculate nCn-r
    if (n - r < r)
        return nCrModPFermat(n, n - r, p);

    // Fill factorial array so that we
    // can find all factorial of r, n
    // and n-r
    unsigned long long res = 1;
    // keep multiplying numerator terms and deviding denominator terms in res
    for (int i = r; i >= 1; i--)
        res = divide(mul(res, n - i + 1, p), i, p);
    return res;
}

vector<bool> sieve(int n)
{
    //Time Complexity:- O(log(log(n)))
    vector<bool> is_prime(n+1, 1);
    is_prime[0] = is_prime[1] = 0;
    for (int i = 2; i <= n; i++) 
    {
        if (is_prime[i] && i*i <= n) 
        {
            for (int j = i*i; j<=n; j+=i)
                is_prime[j] = 0;
        }
    }
    return is_prime;
}


#define auto2 vector<pii >::iterator
#define auto1 vector<vector<int> >::iterator
#define auto map<int, int >::iterator
#define pii pair<int,int> 
#define mem(a,b) memset((a),(b),sizeof(a))
#define mp make_pair

// Code Starts here
const float scale = 1e+6;
float expected(float c,float m,float p,float v)
{
    float ans=p/scale;
    float fc=0,fm=0;
    // cout<<ans<<" ans"<<endl;
    if(c!=0 && m!=0)
    {
        fc=(c/scale)*(1+expected(c-min(c,v),m+min(c,v)/2,p+min(c,v)/2,v));
        fm=(m/scale)*(1+expected(c+min(m,v)/2,m-min(m,v),p+min(m,v)/2,v));
        // cout<<fc<<" "<<fm<<endl;
        return ans+fc+fm;    
    }
    else if(c==0 && m!=0)
    {
        fm=(m/scale)*(1+expected(c,m-min(m,v),p+min(m,v),v));
        // cout<<fm<<endl;
        return ans+fm;
    }
    else if(c!=0 && m==0)
    {
        fc=(c/scale)*(1+expected(c-min(c,v),m,p+min(c,v),v));
        // cout<<fc<<endl;
        return ans+fc;
    }
    else
    {
        return ans;
    }
}

void solve(int tc){
    float c,m,p,v;
    cin>>c>>m>>p>>v;
    // cout<<c<<" "<<m<<" "<<p<<" "<<v<<endl;
    cout<<fixed<<setprecision(8)<<expected(c*scale,m*scale,p*scale,v*scale)<<endl;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    // for(int tc=1;tc<=t;tc++){
    //     cout<<"Case #"<<tc<<": ";
    //     solve();
    // }

    for (int tc = 1; tc <= t; tc++)
    {
        solve(tc);
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game
1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends
302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK
1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm